package arithmetic;

import java.util.*;

public class TestHW16_1 {
    public static void main(String[] args) {
        char ch = '0';
        int i = 1+47;
        int j  = ch-10;
        new ArrayList<Integer>();
        System.out.println((char)i);
//        double d = 1;
//
//        Scanner in = new Scanner(System.in);
//        // 注意 hasNext 和 hasNextLine 的区别
//        int num = in.nextInt();
//        if (num == 0)return;
//        int[] nums = new int[num];
//        for (int i = 0; i < nums.length; i++) {
//            nums[i] = in.nextInt();
//        }
//        if (nums.length == 2) {
//            int num0 = nums[0] + nums[1];
//            boolean f = primeTest(num0);
//            if (f) {
//                System.out.println(1);
//            } else {
//                System.out.println(0);
//            }
//            return;
//        }
//        // int count = group(nums, 0, 0);
//        // System.out.println(count);
//        oddAndEvenNum(nums);
    }

    public static void oddAndEvenNum(int[] nums) {
        //获取奇数数据，偶数数据
        ArrayList<Integer> oddList = new ArrayList<Integer>();
        ArrayList<Integer> evenList = new ArrayList<Integer>();
        int index = 0;
        while (index < nums.length) {
            int num = nums[index];
            if (num % 2 != 1) {
                oddList.add(num);
            } else {
                evenList.add(num);
            }
            index++;
        }
        //排序
        oddList.sort(Comparator.comparingInt(i -> i));
        evenList.sort(Comparator.comparingInt(i -> i));

//        System.out.println(oddList.size() + "\n" + oddList.toString() + "\n" + evenList.size() + "\n" + evenList.toString());
        HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
        int count = group(oddList, 0, evenList, hashMap);
        System.out.println(count);
    }


    public static int group(ArrayList<Integer> oddList, int oddIndex,
                            ArrayList<Integer> evenList, HashMap<Integer, Integer> hashMap) {
        if (oddIndex >= oddList.size()) {
            Set<Integer> set = hashMap.keySet();
            for(Integer i:set){
                System.out.println(hashMap.get(i)+"::"+evenList.get(i));
            }
            return hashMap.size();
        }
        int odd = oddList.get(oddIndex);
        //----------------start-----------
        //-------------end-------------------
        boolean f1 = switchGroup(evenList, 0, odd, hashMap);
        if(odd == 7808){
            System.out.println(f1+""+hashMap.toString());
        }
        oddIndex++;
        return group(oddList, oddIndex, evenList, hashMap);
    }

    public static boolean switchGroup(ArrayList<Integer> evenList,
                                      int start,
                                      int odd, HashMap<Integer, Integer> hashMap) {
        boolean f1 = false;
        for (int i = start; i < evenList.size(); i++) {
            int even = evenList.get(i);
            int add = even + odd;
            boolean f = primeTest(add);
            if (f) {
                if (hashMap.get(i) == null) {
                    hashMap.put(i, odd);
                    f1 = true;
                    break;
                } else {
                    int oddPre = hashMap.get(i);
                    f1 = switchGroup(evenList, i + 1, oddPre, hashMap);
                    if (f1) {
                        hashMap.put(i, odd);
                        break;
                    }
                }
            }
        }
        return f1;
    }


    //判断数字是否是素数
    public static boolean primeTest(int num) {
        boolean f = false;
        if (num == 2) {
            f = true;
            return f;
        }
        //num>2
        int end = num / 2;
        for (int i = 2; i <= end; i++ ) {
            if (num % i == 0) {
                f = false;
                break;
            } else {
                f = true;
            }
        }
        return f;
    }
}
